package com.wd.live.module_my.view.activity

import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hyphenate.EMCallBack
import com.hyphenate.chat.EMClient
import com.hyphenate.exceptions.HyphenateException
import com.wd.live.module_base.net.bean.my.FindPrivateLetterListBean
import com.wd.live.module_base.ui.base.BaseActivity
import com.wd.live.module_my.R
import com.wd.live.module_my.databinding.ActivityPrivateMessageBinding
import com.wd.live.module_my.view.adapter.PrivateAdapter
import com.wd.live.module_my.viewmodel.MyViewModel


@Route(path = "/my/private_message_activity")
class PrivateMessageActivity : BaseActivity<MyViewModel,ActivityPrivateMessageBinding>() {
    val titleArr = mutableListOf<String>("我发起的", "对方发起的")
    var labsPrivateArr = mutableListOf<FindPrivateLetterListBean>()
    override fun getLayout(): Int {
        return R.layout.activity_private_message
    }

    override fun initData() {

        try {
            // 注册失败会抛出 HyphenateException。
            // 同步方法，会阻塞当前线程。
            EMClient.getInstance().createAccount("u_Fk7ku59H", "Wjj20050118.")
            //成功
            //callBack.onSuccess(createLiveData(userName));
        } catch (e: HyphenateException) {
            //失败
            //callBack.onError(e.getErrorCode(), e.getMessage());
        }



            EMClient.getInstance().login("u_Fk7ku59H", "Wjj20050118.", object : EMCallBack {
                override fun onSuccess() {
                    Log.i("TAGooo", "onSuccess: 登录成功")
                    // 登录成功回调
                    EMClient.getInstance().chatManager().loadAllConversations();
                    EMClient.getInstance().groupManager().loadAllGroups();
                }

                override fun onError(code: Int, error: String?) {
                    Log.i("TAGooo", "onSuccess: 失败" + code + error)
                    // 登录失败回调，包含错误信息
                }

            })

            titleArr.forEach { title ->
                binding.tbPrivate.addTab(binding.tbPrivate.newTab().setText(title))
            }

            viewModel.getFindPrivateLetterList()
            viewModel.dataFindPrivateLetterListBean.observe(this) {
                it.result.let { it1 ->
                    if (it1 != null) {
//                    labsPrivateArr.clear()
                        labsPrivateArr.addAll(it1)
                    }
                    binding.rvPrivate.adapter?.notifyDataSetChanged()
                }
                binding.rvPrivate.adapter =
                    PrivateAdapter(this, R.layout.private_item, labsPrivateArr)
                val linearLayoutManager =
                    LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
                binding.rvPrivate.layoutManager = linearLayoutManager
            }
        }
    override fun getViewModelClass(): Class<MyViewModel> {
        return MyViewModel::class.java
    }
    }
